const userUtils = require("../../utils/user");
const validate = require('../../utils/validate')
const common = require('../../utils/common')
const { exceptReceived, getReceivedExtList } = require('../../services/order')

Page({
  data: {
    pickupInfo: {},
    reasonList: [],
    formData: {
      receiveDiffReason: '',
      receivedQty: '',
      id: '',
      abnormal: true
    },
    reasonIndex: 0,
  },
  onLoad: function ({ data }) {
    const pickupInfo = JSON.parse(data) || {}
    this.setData({
      pickupInfo
    }, () => {
      this.updateFormData({ id: pickupInfo.id })
    })
    this.getReasonList()
  },
  updateFormData: function (payload) {
    this.setData({
      formData: { ...this.data.formData, ...payload }
    })
  },
  getReasonList: function () {
    getReceivedExtList().then(res => {
      this.setData({
        reasonList: res.data
      })
    }).catch(err => {
      common.log(err)
    })
  },
  inputChange: function (e) {
    this.updateFormData({ receivedQty: e.detail.value })
  },
  onSelectorChange: function (e) {
    const reasonIndex = e.detail.value
    this.updateFormData({
      receiveDiffReason: this.data.reasonList[reasonIndex]
    })
    this.setData({
      reasonIndex,
    })
  },
  onSubmit: function () {
    const { receiveDiffReason, receivedQty } = this.data.formData
    if (!receivedQty) {
      common.showToast('请填写实际揽收数量！')
      return
    }
    if (receivedQty > this.data.pickupInfo.sendQty) {
      common.showToast('实际揽收数量不能大于应收数量！')
      return
    }
    if (!receiveDiffReason) {
      common.showToast('请选择异常原因！')
      return
    }
    // const deliveryOrderId = this.data.pickupInfo.orderHeaderId
    // const skusList = [this.data.pickupInfo.skuId]
    // const params = { ...this.data.formData, deliveryOrderId, skusList }
    //调用方法，返回上一页
    var pages = getCurrentPages();
    var prePage = pages[pages.length - 2];
    prePage.updateEnableData(this.data.formData);
    wx.navigateBack();
    // exceptReceived(params).then((res) => {
    //   common.showToast(res.message)
    //   this.navigateBack()
    // }).catch((err) => {
    //   common.log(err)
    // })
  }
})
